// ==UserScript==
// @name           sCary's ShugaShack
// @namespace      http://lmnopc.com/greasemonkey/
// @include        http://shacknews.com/*
// @include        http://www.shacknews.com/*
// ==/UserScript==

/* I wrote this to celebrate the 10th anniversary of my Shack account 
	registration date 

	Based from Internet Archive's capture of the Shack on 1999-02-20
	http://web.archive.org/web/19990220173911/http://www.shugashack.com/
	
	- ThomW
*/ 


if ((script_enabled('shugashack') == "true") && (location.href.indexOf('shacknews.com') != -1)) 
{
    // grab start time of script
    var benchmarkTimer = null;
    var scriptStartTime = getTime();
    function getTime() { benchmarkTimer = new Date(); return benchmarkTimer.getTime(); }

	/* ==== GOOGLE CHROME EXTENSION UNIQUE ==== */

	// Add shugashack to the body's class to activate the CSS rules for the shugashack! 
	document.getElementsByTagName('body')[0].className += ' shugashack'; 
	
	function killShugaShack()
	{
		var currentYear = new Date().getFullYear(); 
	
		if (confirm('Do you really want to leave 1999 behind and return to ' + currentYear + '?'))
		{
			alert('lame.');
		
			toggle_script('shugashack', false); 
			location.reload(); 
		} 
	}

	// killswitch!	
	var mdk = document.createElement('a'); 
	mdk.style.position = 'absolute'; 
	mdk.style.top = '0'; 
	mdk.style.right = '0';
	mdk.style.backgroundColor = '#f00'; 
	mdk.style.color = '#fff';
	mdk.style.padding = '4px 10px'; 
	mdk.appendChild(document.createTextNode('[X]')); 
	mdk.addEventListener('click', killShugaShack, false);
	mdk.setAttribute('href', '#');  
	document.getElementsByTagName('body')[0].appendChild(mdk);
	
	/* ==== END UNIQUE ==== */

	/* === GREASEMONKEY SCRIPT ==== */
	
function getElementByClassName(oElm, strTagName, strClassName)
{
	var arrElements = oElm.getElementsByTagName(strTagName);
	for(var i=0; i < arrElements.length; i++)
	{
		if (arrElements[i].className.indexOf(strClassName) == 0)
		{
			return arrElements[i];
		}
	}
}

function getElementsByClassName(oElm, strTagName, strClassName)
{
	var res = new Array(); 
	var arrElements = oElm.getElementsByTagName(strTagName);
	for(var i=0; i < arrElements.length; i++)
	{
		if (arrElements[i].className.indexOf(strClassName) == 0)
		{
			res.push(arrElements[i]); 
		}
	}
	return res; 
}

// Retitle page
document.title = 'Shugashack.com - Game News With Flava!';

// create a better header 
var header = document.createElement('div');
header.setAttribute('id', 'shugashack-header'); 
var a = document.createElement('a'); 
a.href = 'http://www.shugashack.com/';
// CSS MOVED TO SHACKSCRIPTS.CSS 
header.appendChild(a); 

var sectionsDiv = document.getElementById('sections');
sectionsDiv.insertBefore(header, sectionsDiv.firstChild);

// Walk through all the stories and find date changes to create banners 
lastDate = ''; 
stories = getElementsByClassName(getElementByClassName(document.getElementById('section1'), 'div', 'news'), 'div','story'); 
for (i = 0; i < stories.length; i++)
{
	story = stories[i];

	var dateSpan = getElementByClassName(story, 'span', 'date'); 
	if (dateSpan)
	{
		storyDateTime = dateSpan.innerHTML;
		
		storyDateTime = storyDateTime.split(' '); 
		storyDate = storyDateTime[0] + ' ' + storyDateTime[1] + ' ' + storyDateTime[2];
		storyTime = storyDateTime[3];
		
		// Add time span after the <h1> tag
		storyTitle = story.getElementsByTagName('h1'); 
		if (storyTitle.length)
		{
			storyTitle = storyTitle[0];
		
			var timeStamp = document.createElement('span');
			timeStamp.className = 'storyTimestamp';  
			timeStamp.appendChild(document.createTextNode('[' + storyTime + ']'));
			
			storyTitle.appendChild(timeStamp);  
		}
		
		// Ad bar 
		if (storyDate != lastDate)
		{
			var dateBar = document.createElement('div'); 
			dateBar.className = "dateBar"; 
			dateBar.appendChild(document.createTextNode(storyDate)); 
			
			story.parentNode.insertBefore(dateBar, story); 
		}
		lastDate = storyDate; 
	}
}

	/* ==== END GREASEMONKEY ==== */

    // log execution time
    tw_log('shugashack.js: ' + location.href + ' / ' + (getTime() - scriptStartTime) + 'ms');
	
} 